using Colorful.Database;
using Colorful.Image;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.International.Converters.PinYinConverter;
using Colorful.Apps.ERP;
using Colorful.Apps.ERP.Models;
using Colorful.Models;
using Colorful.CMS.Core;
using System.Text;

namespace Colorful.Apps.Erp.Controller
{
    [Route("api/erp")]
    public class ErpController : AppMemberController
    {
        private const int PAGESIZE = 8;

        #region 产品

        #region 获取产品Tree
        [Route("product/list/tree")]
        public IActionResult GetSelectProducts(string ids)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                //var products = db.Products.Select(a => new Product() { Id = a.Id, Name = a.Name, Photo = a.Photo, List = a.List }).ToList().Select(a => new
                //Tree<string>()
                //{
                //    id = a.Id,
                //    text = a.Name,
                //    icon = a.Photo,
                //    children = a.List.Select(b => new
                //    Tree<string>()
                //    {
                //        id = b.Id,
                //        text = $" {b.Spec}/{b.Price}元/余:{b.Amount}",
                //        Checked = ids.Contains(b.Id)
                //    }).ToList()
                //}).ToList();
                //return this.GetTree<string>(products);
                var products = db.Products.Select(a => new Product() { Id = a.Id, Name = a.Name, Photo = a.Photo, List = a.List }).ToList().Select(a => new
                {
                    id = a.Id,
                    text = a.Name,
                    icon = a.Photo,
                    children = a.List.Select(b => new
                    {
                        id = b.Id,
                        spec = b.Spec,
                        price = b.Price,
                        referencePrice = b.ReferencePrice,
                        amount = b.Amount,
                        Checked = ids.Contains(b.Id),
                        remark = b.Remark,
                        discount = b.Discount,
                        parentName = a.Name, //前台订单显示时用
                        parentId = a.Id  //前台订单提交时用
                    }).ToList()
                }).ToList();

                return this.GetJsonResult(products);
            }
        }
        #endregion

        #region 获取列表
        [Route("product/list")]
        public IActionResult GetProductData(string keyword, int page)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var query = db.Products.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.Name.Contains(keyword));
                var datas = query.OrderByDescending(a => a.ByOrder).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.Name, a.Price, a.Photo }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #region 产品详细
        [Route("product/info")]
        public IActionResult GetProduct(string id)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var data = db.Products.FirstOrDefault(a => a.Id == id);
                return this.GetResult(true, data);
            }
        }
        #endregion
        #region 添加/编辑产品
        [Route("product/save")]
        public IActionResult SaveProductData(string id, string name, int sortId, string dateRange)
        {
            if (string.IsNullOrEmpty(name))
                return this.GetResult(false, "产品名称不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = ERP.DbHelper.GetDb())
                {

                    Product data;
                    if (isNew)
                    {
                        data = new Product()
                        {
                            ByOrder = db.Products.GetId(),
                            Flags = new List<int>(),
                            List = new List<ProductShow>(),
                            Tags = new List<string>()
                        };
                    }
                    else
                        data = db.Products.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(data, new DisableField("Code"),
                        new RequireField("SortId", "产品类别"),
                        new RequireField("Name", "产品名称"),
                        new ImageField("Photo")
                        {
                            Required = true,
                            Text = "产品缩略图",
                            MaxSize = new System.Drawing.Size(220, 220),
                            MaxLength = 4096,
                            CompressQuality = 90,
                            SavePath = $"/upFiles/erp/product/{data.Id}",
                        },
                        new ImageField("Images")
                        {
                            SavePath = $"/upFiles/erp/product/{data.Id}",
                            AllowExtensions = new string[] { ".jpg", ".png", ".mp4" },
                            Text = "产品展示图",
                            MinCount = 1,
                            MaxCount = 10,
                            MaxLength = 51200
                        });
                    if (isNew)
                        data.Code = $"{db.Codes.Where(a => a.Id == sortId).Select(a => a.CodeName).FirstOrDefault()}{data.ByOrder.ToString().PadLeft(2, '0')}";
                    data.LastModify = DateTime.Now;
                    data.ModifyUser = this.LoginId;
                    var namePY = PinYinHelper.GetPingYin(data.Name);
                    data.Keyword = $"{data.Name} {data.Code} {namePY.FirstPingYin} {namePY.TotalPingYin}";
                    if (!string.IsNullOrEmpty(data.Place))
                        data.Keyword += " " + data.Place;
                    if (!string.IsNullOrEmpty(data.Brand))
                        data.Keyword += " " + data.Brand;
                    db.Products.Save(data);
                    return this.GetResult(true, data);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 产品类别
        [Route("product/sorttype")]
        public IActionResult GetSortType()
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var codes = db.Codes.Where(a => a.MenuId == (int)ERP.Models.MenuSort.ProductSort).Select(a => new
                {
                    id = a.Id,
                    text = a.Name
                }).ToArray();
                return this.GetJsonResult(new
                {
                    Codes = codes,
                    ProductFlags = EnumHelper.ToJsonData<ProductFlags>()
                });
            }
        }
        #endregion
        #region 产品规格保存
        [Route("productshow/save")]
        public IActionResult SaveProductShowData(string id, string productId)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    var list = db.Products.Where(a => a.Id == productId).Select(a => a.List).FirstOrDefault();
                    if (list == null) list = new List<ProductShow>();
                    ProductShow ps;
                    if (string.IsNullOrEmpty(id))
                    {
                        ps = new ProductShow()
                        {
                            Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString()
                        };
                        list.Add(ps);
                    }
                    else
                        ps = list.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(ps);

                    db.Products.Update(a => a.Id == productId, b => b.List, list);
                    return this.GetResult(true, ps);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 删除
        [Route("product/delete")]
        public IActionResult DeleteProductData(string id, string productId)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    if (!string.IsNullOrEmpty(productId))
                    {
                        if (string.IsNullOrEmpty(id))
                        {
                            FormHelper.DeleteDirectory($"/upFiles/erp/product/{productId}");
                            db.Products.Delete(a => a.Id == productId);
                        }
                        else
                        {
                            var list = db.Products.Where(a => a.Id == productId).Select(a => a.List).FirstOrDefault();
                            var item = list.FirstOrDefault(a => a.Id == id);
                            list.Remove(item);
                            db.Products.Update(a => a.Id == productId, b => b.List, list);
                        }
                    }
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #endregion
        #region 客户
        #region 获取列表
        [Route("client/list")]
        public IActionResult GetClientData(string keyword, int page)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var query = db.Clients.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.Name.Contains(keyword));
                var datas = query.OrderByDescending(a => a.AddDate).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.Name }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #region 详细
        [Route("client/info")]
        public IActionResult GetClient(string id)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var data = db.Clients.FirstOrDefault(a => a.Id == id);
                return this.GetResult(true, data);
            }
        }
        #endregion
        #region 保存
        [Route("client/save")]
        public IActionResult SaveClientData(string id, string name)
        {
            if (string.IsNullOrEmpty(name))
                return this.GetResult(false, "客户名称不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = ERP.DbHelper.GetDb())
                {

                    ERP.Models.Client data;
                    if (string.IsNullOrEmpty(id))
                    {
                        data = new ERP.Models.Client()
                        {
                            Agents = new List<string>()
                        };
                    }
                    else
                        data = db.Clients.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(data, new DisableField("Agents"), new DisableField("OpenId"), new ImageField("Photo")
                    {
                        SavePath = $"/upFiles/erp/client/{data.Id}",
                        Required = false,
                        AllowExtensions = new string[] { ".jpg", ".jpeg", ".png" },
                        Text = "客户照片",
                        MinCount = 1,
                        MaxCount = 1,
                        MaxLength = 2048
                    });
                    db.Clients.Save(data);
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 客户类型
        [Route("client/type")]
        public IActionResult GetClientType()
        {
            return this.GetResult(true, EnumHelper.ToJsonData<ClientType>());
        }
        #endregion
        #region 上级代理
        [Route("client/GetAgents")]
        public IActionResult GetAgents()
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var members = db.Clients.Select(a => new Colorful.Apps.Models.Member()
                {
                    Id = a.Id,
                    Name = a.Name
                }).ToList();
                return this.GetResult(true, members.Select(a => new
                {
                    id = a.Id,
                    text = a.Name
                }));
            }
        }
        #endregion
        #region 删除
        [Route("client/delete")]
        public IActionResult DeleteClientData(string id)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        FormHelper.DeleteDirectory($"/upFiles/erp/client/{id}");
                        db.Clients.Delete(a => a.Id == id);
                    }
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #endregion
        #region 进货
        #region 获取列表
        [Route("Purchase/list")]
        public IActionResult GetPurchaseData(string keyword, int page)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var query = db.Purchases.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.ProductId.Contains(keyword));
                var datas = query.OrderByDescending(a => a.AddDate).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.ProductId, a.Cost }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #region 详细
        [Route("Purchase/info")]
        public IActionResult GetPurchase(string id)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var data = db.Purchases.FirstOrDefault(a => a.Id == id);
                return this.GetResult(true, data);
            }
        }
        #endregion
        #region 保存
        [Route("Purchase/save")]
        public IActionResult SavePurchaseData(string id, string productId)
        {
            if (string.IsNullOrEmpty(productId))
                return this.GetResult(false, "产品Id不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = ERP.DbHelper.GetDb())
                {

                    Purchase data;
                    if (string.IsNullOrEmpty(id))
                        data = new Purchase();
                    else
                        data = db.Purchases.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(data);
                    db.Purchases.Save(data);
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 客户类型
        [Route("Purchase/getProductId")]
        public IActionResult GetProductId()
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var datas = db.Products.Select(a => new Product()
                {
                    Id = a.Id,
                    Name = a.Name
                }).ToList();
                return this.GetResult(true, datas.Select(a => new
                {
                    id = a.Id,
                    text = a.Name
                }));
            }
        }
        #endregion
        #region 删除
        [Route("Purchase/delete")]
        public IActionResult DeletePurchaseData(string id)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        db.Purchases.Delete(a => a.Id == id);
                    }
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #endregion
        #region 库存
        #region 获取列表
        [Route("Stock/list")]
        public IActionResult GetStockData(string keyword, int page)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var query = db.Stocks.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.ProductId.Contains(keyword));
                var datas = query.OrderByDescending(a => a.AddDate).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.Spec }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #region 详细
        [Route("Stock/info")]
        public IActionResult GetStock(string id)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var data = db.Stocks.FirstOrDefault(a => a.Id == id);
                return this.GetResult(true, data);
            }
        }
        #endregion
        #region 保存
        [Route("Stock/save")]
        public IActionResult SaveStockData(string id, string productId)
        {
            if (string.IsNullOrEmpty(productId))
                return this.GetResult(false, "产品Id不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = ERP.DbHelper.GetDb())
                {

                    Stock data;
                    if (string.IsNullOrEmpty(id))
                    {
                        data = new Stock()
                        {
                            Features = new List<string>()
                        };
                    }
                    else
                        data = db.Stocks.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(data);
                    db.Stocks.Save(data);
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 删除
        [Route("Stock/delete")]
        public IActionResult DeleteStockData(string id)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        db.Stocks.Delete(a => a.Id == id);
                    }
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #endregion
        #region 供货商
        #region 获取列表
        [Route("Vender/list")]
        public IActionResult GetVenderData(string keyword, int page)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var query = db.Venders.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.Name.Contains(keyword));
                var datas = query.OrderByDescending(a => a.AddDate).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.Name }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #region 详细
        [Route("Vender/info")]
        public IActionResult GetVender(string id)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var data = db.Venders.FirstOrDefault(a => a.Id == id);
                var products = db.Products.Select(a => new Product() { Name = a.Name, List = a.List }).ToList().Select(a => new
                {
                    title = a.Name,
                    parent = true,
                    expand = true,
                    children = a.List.Select(b => new
                    {
                        id = b.Id,
                        title = $"{b.Spec}/{b.Price}元({b.ReferencePrice})/余：{b.Amount}",
                        @checked = data.Products.Contains(b.Id),
                        parent = false
                    })
                });
                return this.GetJsonResult(new
                {
                    Data = data,
                    List = products
                });
            }
        }
        #endregion
        #region 保存
        [Route("Vender/save")]
        public IActionResult SaveVenderData(string id, string name, string ids)
        {
            if (string.IsNullOrEmpty(name))
                return this.GetResult(false, "供货商名称不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = ERP.DbHelper.GetDb())
                {

                    Vender data;
                    if (string.IsNullOrEmpty(id))
                    {
                        data = new Vender()
                        {
                            Products = new List<string>()
                        };
                    }
                    else
                        data = db.Venders.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(data);
                    var list = new List<string>();
                    foreach (var item in ids.Split(','))
                    {
                        list.Add(item);
                    }
                    data.Products = list;
                    db.Venders.Save(data);
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 删除
        [Route("Vender/delete")]
        public IActionResult DeleteVenderData(string id)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        db.Venders.Delete(a => a.Id == id);
                    }
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #endregion
        #region 订单管理
        #region 获取列表
        [Route("Order/list")]
        public IActionResult GetOrderData(string keyword, int page)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var query = db.Orders.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.Remark.Contains(keyword));
                var datas = query.OrderByDescending(a => a.AddDate).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new { a.Id, a.Remark }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #region 详细
        [Route("Order/info")]
        public IActionResult GetOrder(string id)
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var data = db.Orders.FirstOrDefault(a => a.Id == id);
                return this.GetResult(true, data);
            }
        }
        #endregion
        #region 保存
        [Route("Order/save")]
        public IActionResult SaveOrderData(string id, string clientId, int productCount, OrderState state)
        {
            if (string.IsNullOrEmpty(clientId))
                return this.GetResult(false, "客户不能为空!");
            try
            {
                var isNew = string.IsNullOrEmpty(id);
                using (var db = ERP.DbHelper.GetDb())
                {
                    Order data;
                    if (string.IsNullOrEmpty(id))
                    {
                        data = new Order()
                        {
                            ByOrder = db.Orders.GetId()
                        };
                    }
                    else
                        data = db.Orders.FirstOrDefault(a => a.Id == id);
                    FormHelper.FillTo(data);
                    decimal total = 0;
                    #region 添加产品列表
                    data.Products = new List<OrderProduct>();
                    for (var i = 0; i < productCount; i++)
                    {
                        var parentId = Request.Form["ParentId" + i];
                        var proId = Request.Form["Id" + i];
                        var price = decimal.Parse(Request.Form["Price" + i]);
                        var amount = Int32.Parse(Request.Form["Amount" + i]);
                        var remark = Request.Form["Remark" + i];
                        var discount = decimal.Parse(Request.Form["Discount" + i]);
                        data.Products.Add(new OrderProduct()
                        {
                            Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString(),
                            ProductId = proId,
                            Price = price,
                            Amount = amount,
                            Discount = discount,
                            Remark = remark
                        });
                        total += (price * amount) * discount; //计算总金额
                        #region 减库存
                        var list = db.Products.Where(a => a.Id == parentId).Select(a => a.List).FirstOrDefault();
                        list.Where(a => a.Id == proId).FirstOrDefault().Amount = list.Where(a => a.Id == proId).FirstOrDefault().Amount - amount;
                        db.Products.Update(a => a.Id == parentId, b => b.List, list);
                        #endregion
                    }
                    #endregion

                    data.Price = total;
                    data.State = state;
                    db.Orders.Save(data);
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 删除
        [Route("Order/delete")]
        public IActionResult DeleteOrderData(string id)
        {
            try
            {
                using (var db = ERP.DbHelper.GetDb())
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        db.Orders.Delete(a => a.Id == id);
                    }
                    return this.GetResult(true);
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion
        #region 订单状态
        [Route("Order/getOrderState")]
        public IActionResult GetOrderState()
        {
            return this.GetResult(true, EnumHelper.ToJsonData<OrderState>());
        }
        #endregion
        #region 获取客户Id
        [Route("order/GetClients")]
        public IActionResult GetClientId()
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                var datas = db.Clients.Select(a => new ERP.Models.Client()
                {
                    Id = a.Id,
                    Name = a.Name
                }).ToList();
                return this.GetResult(true, datas.Select(a => new
                {
                    id = a.Id,
                    text = a.Name
                }));
            }
        }
        #endregion
        #region 获取产品分类
        [Route("OrderCategory/list")]
        public IActionResult GetOrderCategoryData(string keyword, int page, string ids = "")
        {
            using (var db = ERP.DbHelper.GetDb())
            {
                //var products = db.Products.Select(a => new Product() { Id = a.Id, Name = a.Name, Photo = a.Photo, List = a.List }).ToList().Select(a => new
                //{
                //    id = a.Id,
                //    text = a.Name,
                //    icon = a.Photo,
                //    children = a.List.Select(b => new
                //    {
                //        id = b.Id,
                //        text = $" {b.Spec}/{b.Price}元/余:{b.Amount}",
                //        price = b.Price,
                //        amount = b.Amount,
                //        Checked = ids.Contains(b.Id),
                //        remark = b.Remark,
                //        discount = b.Discount,
                //        parentName = a.Name, //前台订单显示时用
                //        parentId = a.Id  //前台订单提交时用
                //    }).ToList()
                //}).ToList();

                //return this.GetJsonResult(products);

                var query = db.Products.GetQuery();
                if (!string.IsNullOrEmpty(keyword))
                    query = query.Where(a => a.Keyword.Contains(keyword));
                var datas = query.OrderByDescending(a => a.AddDate).Skip(page * PAGESIZE).Take(PAGESIZE).Select(a => new Product() { Id = a.Id, Name = a.Name, Photo = a.Photo, List = a.List }).ToList().Select(a => new
                {
                    id = a.Id,
                    text = a.Name,
                    icon = a.Photo,
                    children = a.List.Select(b => new
                    {
                        id = b.Id,
                        price = b.Price,
                        spec = b.Spec,
                        referencePrice = b.ReferencePrice,
                        amount = b.Amount,
                        text = $" {b.Spec}/{b.Price}元/余:{b.Amount}",
                        Checked = ids.Contains(b.Id),
                        discount = b.Discount,
                        parentName = a.Name, //前台订单显示时用
                        parentId = a.Id  //前台订单提交时用
                    }).ToList()
                }).ToList();
                return this.GetResult(true, new Pager(page, query.Count(), PAGESIZE, datas));
            }
        }
        #endregion
        #endregion
    }
}
